package cn.qf.shop.controller;

import cn.qf.shop.pojo.entity.Community;
import cn.qf.shop.pojo.vo.RespResult;
import cn.qf.shop.service.CommunityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@EnableSwagger2
@RestController
@RequestMapping("/Community")
@RestControllerAdvice
@Api(tags = "小区业务接口")
public class CommunityController extends BaseController {
    private Logger logger = (Logger) LoggerFactory.getLogger(CommunityController.class);
    @Resource
    CommunityService communityService;

    @ResponseBody
    @ApiOperation("新增单个小区")
    @PostMapping(value = "/addCommunity")
    public RespResult addCommunity(@RequestBody Community community){
        RespResult respResult = communityService.addCommunity(community);
        if (respResult == null){
            super.returnRespWithFailed("新增小区失败");
        }
        logger.toString();
        return respResult;
    }

    @ResponseBody
    @ApiOperation("删除单个小区")
    @PostMapping(value = "/delCommunity")
    public RespResult delCommunity(int id){
        RespResult respResult = communityService.delCommunity(id);
        if (respResult == null){
            super.returnRespWithFailed("删除小区失败");
        }
        logger.toString();
        return respResult;
    }

    @ResponseBody
    @ApiOperation("批量删除小区")
    @PostMapping(value = "/delCommunityBatch")
    public RespResult delCommunityBatch(@RequestBody List<String> idList){
        RespResult respResult = communityService.delCommunityBatch(idList);
        if (respResult == null){
            super.returnRespWithFailed("删除小区失败");
        }
        logger.toString();
        return respResult;
    }

    @ResponseBody
    @ApiOperation("修改小区信息")
    @PostMapping(value = "/updateCommunity")
    public RespResult updateCommunity(@RequestBody Community community){
        RespResult respResult = communityService.updateCommunity(community);
        if (respResult == null){
            super.returnRespWithFailed("修改小区信息失败");
        }
        logger.toString();
        return respResult;
    }

    @ResponseBody
    @ApiOperation("查询所有小区信息")
    @PostMapping(value = "/checkAllCommunity")
    public RespResult checkAllCommunity(){
        List<Community> list = communityService.checkAll();
        RespResult respResult = super.returnRespWithSuccess(list);
        logger.toString();
        return respResult;
    }

    @ResponseBody
    @ApiOperation("根据时间查询小区信息")
    @PostMapping(value = "/checkCommunityByTime")
    public RespResult checkCommunityByTime(@RequestParam("beginDate") Date beginDate, @RequestParam("endDate") Date endDate){
        List<Community> list =communityService.checkByTime(beginDate,endDate);
        RespResult respResult = super.returnRespWithSuccess(list);
        logger.toString();
        return respResult;
    }
}
